package demo1;

import java.util.Arrays;
import java.util.Comparator;

public class Test {
    public static void test1() {
        int[] array = new int[]{1,2,3,4,3,2,6,3,8,4};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
    public static void test2() {
        People[] people = new People[3];
        people[0]= new People(18,"zhangsan");
        people[1] = new People(10,"lisi");
        people[2] = new People(22,"wangwu");
        System.out.println(Arrays.toString(people));
        Arrays.sort(people);
        System.out.println("-------------");
        System.out.println(Arrays.toString(people));

    }
    public static void test3() {
        People[] peoples = new People[3];
        peoples[0]= new People(18,"zhangsan");
        peoples[1] = new People(10,"lisi");
        peoples[2] = new People(22,"wangwu");
        System.out.println("------原始顺序-------");
        System.out.println(Arrays.toString(peoples));
        NameCompare nameCompare = new NameCompare();
        Arrays.sort(peoples,nameCompare);
        System.out.println("------按姓名排序-------");
        System.out.println(Arrays.toString(peoples));
        System.out.println("------按年龄排序-------");
        AgeCompare ageCompare = new AgeCompare();
        Arrays.sort(peoples,ageCompare);
        System.out.println(Arrays.toString(peoples));
    }
    public static void BubbleSort(Comparable[] comparables,Comparator comparator) {//发生向上转型
        for (int i = 0; i < comparables.length; i++) {
            for (int j = i; j <comparables.length-1 ; j++) {
                if(comparator.compare(comparables[j],comparables[j+1]) > 0) {
                    Comparable temp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = temp;
                }
            }
        }
    }
    public static void BubbleSort1(People[] people,Comparator<People> comparator) {//发生向上转型
        for (int i = 0; i < people.length; i++) {
            for (int j = i; j <people.length-1 ; j++) {
                if(comparator.compare(people[j],people[j+1]) > 0) {
                    People temp = people[j];
                    people[j] = people[j+1];
                    people[j+1] = temp;
                }
            }
        }
    }

    public static void test4() {
        People[] people1 = new People[3];
        people1[0]= new People(18,"zhangsan");
        people1[1] = new People(10,"lisi");
        people1[2] = new People(22,"wangwu");
        AgeCompare ageCompare = new AgeCompare();
        BubbleSort1(people1,ageCompare);
        System.out.println("------按年龄排序-------");
        System.out.println(Arrays.toString(people1));
    }
    public static void main(String[] args) {
        test4();
    }
}
